package com.test.webmagic;

import com.test.util.DownloadUtil;
import com.test.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lizhilong
 * @create 2020/1/6 16:07
 * @desc 第一个练习，熟悉使用方式，不注重实现方式和实现思想
 */
@Slf4j
public class SpiderImages implements PageProcessor{

    //页面URL的正则表达式
    private static String REGEX_PAGE_URL = "http://www\\.win4000\\.com/zt/gaoqing_\\w+.html";
    //爬取的页数
    private static final  int PAGE_SIZE = 2;

    private static boolean addFalg = false;


    @Override
    public void process(Page page) {
        if(!addFalg){
            List<String> SpidertURL = new ArrayList<String>();
            //添加到目标url中
            for (int i = 2; i < PAGE_SIZE; i++){
                SpidertURL.add("http://www.win4000.com/zt/gaoqing_" + i + ".html");
            }
            //添加url到请求中
            page.addTargetRequests(SpidertURL);
            addFalg = true;
        }

        if (page.getUrl().regex(REGEX_PAGE_URL).match()) {
            //获得所有详情页的连接
            //List<String> detailURL = page.getHtml().xpath("//ul[@class='clearfix']/li/a").links().all();
            List<String> detailURL = page.getHtml().xpath("//div[@class='list_cont Left_list_cont  Left_list_cont1']/div[@class='tab_tj']/div[@class='tab_box']/div/ul[@class='clearfix']/li/a").links().all();
            page.addTargetRequests(detailURL);
        } else {//详情页
            String detailUrl = page.getUrl().toString();
            log.info("详情页地址：{}",detailUrl);

            String picUrl = page.getHtml().xpath("//div[@class='pic-meinv']/a").css("img", "src").toString();
            log.info("图片下载地址：{}",picUrl);

            String suffixName = picUrl.substring(picUrl.lastIndexOf("."),picUrl.length());
            log.info("图片后缀名：{}",picUrl);

            String allPic = page.getHtml().xpath("//div[@class='ptitle']/em/text()").toString();
            String currentIndex = page.getHtml().xpath("//div[@class='ptitle']/span/text()").toString();
            log.info("当前下载{}/{} 张图片",currentIndex,allPic);

            String title = page.getHtml().xpath("//div[@class='ptitle']/h1/text()").toString();
            String picName = StringUtil.getImageName(title,currentIndex,suffixName);

            if(allPic!= null && "1".equals(currentIndex)){
                Integer pageindex = Integer.parseInt(allPic);
                List<String>otherPic = new ArrayList<String>();
                for(int i=2;i<=pageindex;i++){
                    otherPic.add(detailUrl.replaceAll(".html", "_"+i+".html"));
                }
                page.addTargetRequests(otherPic);
            }
            try {
                 DownloadUtil.download( picUrl,picName, "C:\\Image\\");
            } catch (Exception e) {
                log.error(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    @Override
    public Site getSite() {
        return Site.me();
    }


    public static void main(String[] args) {
        Spider picSpider = Spider.create(new SpiderImages()).addUrl("http://www.win4000.com/zt/gaoqing_1.html")
                .thread(5);
        picSpider.start();
    }
}
